package com.test.util;

import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.text.SimpleDateFormat;
import java.util.Date;

import org.apache.struts.upload.FormFile;

public class FileUpLoad {
	
	 public static String upload(String dir, FormFile formFile) throws Exception {

         Date date = new Date();

         String fname = formFile.getFileName();

         //取文件的后缀

         int i = fname.indexOf(".");

         String type = fname.substring(i + 1);
        

         //用时间给文件赋新名称

         String name = String.valueOf(date.getTime());

         fname = name + "." + type;

       
     
         InputStream streamIn = formFile.getInputStream(); // 创建读取用户上传文件的对象
         
     	String yearStr = new SimpleDateFormat("yyyy").format( new Date());
		String monthStr = new SimpleDateFormat("MM").format(new Date());
		String dayStr = new SimpleDateFormat("dd").format(new Date());
      String    rPath = yearStr+"/"+monthStr+"/"+dayStr+"/";
         File uploadFile = new File(dir + "/"+rPath); // 创建把上传数据写到目标文件的对象

        

          // 判断指定路径是否存在，不存在则创建路径

         if (!uploadFile.exists() || uploadFile == null) {

                uploadFile.mkdirs();

         }

         String path = uploadFile.getPath() + "/" + fname;

         OutputStream streamOut = new FileOutputStream(path);

         int bytesRead = 0;

         byte[] buffer = new byte[8192];

         while ((bytesRead = streamIn.read(buffer, 0, 8192)) != -1) {

                streamOut.write(buffer, 0, bytesRead);

         }

         streamOut.close();

         streamIn.close();

         formFile.destroy();

         return "upload/"+rPath+fname;

  }
	 public static String upload1(String dir, FormFile formFile) throws Exception {

         Date date = new Date();

         String fname = formFile.getFileName();

         //取文件的后缀

         int i = fname.indexOf(".");

         String type = fname.substring(i + 1);
        

         //用时间给文件赋新名称

         String name = String.valueOf(date.getTime());

       //  fname = name + "." + type;

       

         InputStream streamIn = formFile.getInputStream(); // 创建读取用户上传文件的对象

         File uploadFile = new File(dir); // 创建把上传数据写到目标文件的对象

        

          // 判断指定路径是否存在，不存在则创建路径

         if (!uploadFile.exists() || uploadFile == null) {

                uploadFile.mkdirs();

         }

         String path = uploadFile.getPath() + "/" + fname;

         OutputStream streamOut = new FileOutputStream(path);

         int bytesRead = 0;

         byte[] buffer = new byte[8192];

         while ((bytesRead = streamIn.read(buffer, 0, 8192)) != -1) {

                streamOut.write(buffer, 0, bytesRead);

         }

         streamOut.close();

         streamIn.close();

         formFile.destroy();

         return fname;

  }
}


